const express = require('express')
const db = require('../database/db')
const url = require('../utils/env')

// 引入路由规则
const router = express.Router()

// 查询产品种类
router.get('/product_kind', (req, res) => {
  const sql = 'SELECT * FROM product_kind WHERE 1 = 1'

  const params = []

  db(sql, params, (result, err) => {
    if(err) {
      return res.send({
        code: 500,
        err,
        msg: '查询失败',
      })
    }

    const data = result

    // 将图片地址拼接上url
    data.forEach(item => {
      item.url = url + '/img' + item.url
    })

    res.send({
      code: 200,
      data,
      msg: '查询成功',
    })
  })
})

// 查询回收方式
router.get('/recovery_mode', (req, res) => {
  const sql = 'SELECT * FROM recovery_mode WHERE 1 = 1'

  const params = []
  
  db(sql, params, (result, err) => {
    if(err) {
      return res.send({
        code: 500,
        err,
        msg: '查询失败',
      })
    }
    
    const data = result
    
    // 将图片地址拼接上url
    data.forEach(item => {
      item.icon = url + '/img/icon' + item.icon
    })

    res.send({
      code: 200,
      data,
      msg: '查询成功',
    })
  })
})


// 将路由规则导出
module.exports = router

